package com.jay.example.IndoorPositioningSystem;

/****
 * https://gitee.com/unw1900/Indoor_Positioning_System
 */
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/****
 * 定义一个View，使用
 * <com.jay.example.sensordemo2.MyView
 * ...
 * </com.jay.example.sensordemo2.MyView>
 * 添加到XML布局。
 *
 * 现实与地图的比例： 1 ：30
 *             单位：米 ：dp
 * 比例于 {@link #addStep(float, float)} 中更改
 */
public class MyView extends View {
//    private Paint mPaintA,mPaintB;
//    private Path mPath;
    private double heading;
    private double stepSize;
    private float mPhase = 2;


//    private final float originX = 50+828+100-45;//起始点位
//    private final float originY = 50+300;
    private final float originX = 500;//起始点位
    private final float originY = 500;


    private float angle = 50;
    private float lastStepX = originX;
    private float lastStepY = originY;

    private float nowStepX = 0;
    private float nowStepY = 0;
    private float destination_X = lastStepX;
    private float destination_Y = lastStepY;

    private float test = 20;
    private boolean flag = true;

    Paint mPaintA = new Paint();
    Paint mPaintB = new Paint();
    Paint mPaintC = new Paint();
    Paint mPaintD = new Paint();
    Paint mPaintF = new Paint();
    Paint mPaintH = new Paint();
    Path mPath = new Path();
    Path mapBound = new Path();
    Path destinationPath = new Path();
    private float lastStepXOfArrow;
    private float lastStepYOfArrow;
    private float[] ofArrow = new float[2];
    private boolean setDestination = false;


    public MyView(Context context) {
        super(context);

//        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        init();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        init();
    }

    private void init(){

//        mPaintA = new Paint();

        //mPaintA 画地图模型
        mPaintA.setAntiAlias(true);          //抗锯齿
        mPaintA.setColor(Color.BLACK);//画笔颜色
        mPaintA.setStyle(Paint.Style.STROKE);  //画笔风格
        mPaintA.setTextSize(60);             //绘制文字大小，单位px
        mPaintA.setStrokeWidth(5);           //画笔粗细



        //mPaintB 标记当前位置
        mPaintB.setAntiAlias(true);          //抗锯齿
        mPaintB.setColor(Color.RED);//画笔颜色
        mPaintB.setStyle(Paint.Style.STROKE);  //画笔风格
        mPaintB.setTextSize(30);             //绘制文字大小，单位px
        mPaintB.setStrokeWidth(5);           //画笔粗细

        //mPaintC 画行进轨迹
        mPaintC.setAntiAlias(true);          //抗锯齿
        mPaintC.setColor(Color.BLUE);//画笔颜色
        mPaintC.setStyle(Paint.Style.FILL_AND_STROKE);  //画笔风格
        mPaintC.setTextSize(40);             //绘制文字大小，单位px
        mPaintC.setStrokeWidth(2);           //画笔粗细

        //mPaintD 画目的地线路
        mPaintD.setAntiAlias(true);          //抗锯齿
        mPaintD.setColor(Color.GREEN);//画笔颜色
        mPaintD.setStyle(Paint.Style.STROKE);  //画笔风格
        mPaintD.setTextSize(20);             //绘制文字大小，单位px
        mPaintD.setStrokeWidth(10);           //画笔粗细

        //mPaintF 画房间
        mPaintF.setAntiAlias(true);          //抗锯齿
        mPaintF.setColor(Color.GRAY);//画笔颜色
        mPaintF.setStyle(Paint.Style.FILL);  //画笔风格
        mPaintF.setTextSize(20);             //绘制文字大小，单位px
        mPaintF.setStrokeWidth(10);           //画笔粗细

        //天井
        mPaintH.setAntiAlias(true);          //抗锯齿
        mPaintH.setColor(Color.LTGRAY);//画笔颜色
        mPaintH.setStyle(Paint.Style.FILL);  //画笔风格
        mPaintH.setTextSize(20);             //绘制文字大小，单位px
        mPaintH.setStrokeWidth(10);           //画笔粗细
//        mPath.moveTo(originX+200,originY+200);
//        PathEffect pathEffect = new PathDashPathEffect(mPath, 12,0, PathDashPathEffect.Style.ROTATE);
//        mPaintB.setPathEffect(pathEffect);
    }

    /****
     * 将 航向角 和 步长 从极坐标系转化为直角坐标系，获取 X , Y值。
     * @param heading 航向角
     * @param stepSize 步长
     */
    public float[] addStep(float heading, float stepSize){
        this.heading = Math.toRadians(heading-180);//将数字转换为角度,-25
        this.stepSize = stepSize * 0.01 * 20;//现实与地图坐标转换
        nowStepX = (float) - Math.round(this.stepSize * Math.sin(this.heading));
        nowStepY = (float) - Math.round(this.stepSize * Math.cos(this.heading));

        double headingOfArrow = Math.toRadians(heading-180);
        ofArrow[0] = (float) - Math.round(this.stepSize * Math.sin(headingOfArrow));
        ofArrow[1] = (float) - Math.round(this.stepSize * Math.cos(headingOfArrow));
//        stepFlag = true;
        return ofArrow;
    }

    public float screenMove(){//移动屏幕，暂不用
        float centerOfScreen = 432;
        return this.lastStepX - centerOfScreen;
    }

    //重写该方法，在这里绘图
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.init();

//        canvas.rotate(50);
        //当前位置向左移动过半时，移动画布（向左），确保当前位置居中
        if (screenMove() < 0)
            canvas.translate(-(screenMove()),0);
        canvas.drawColor(Color.WHITE);   //设置画布背景颜色
        canvas.drawRect(-300, 50, 1050, 1000, mPaintA);            //画矩形



        //绘制边框
        mPath.moveTo(lastStepX,lastStepY);
        mPath.lineTo(nowStepX+lastStepX,nowStepY+lastStepY);//行走路线
        canvas.drawPoint(nowStepX+lastStepX, nowStepY+lastStepY, mPaintB);//当前位置

        //绘制室内地图边界
//        mapBound.moveTo(originX,originY);//起始
//
//        mapBound.lineTo(210+originX,0+originY);
//        mapBound.lineTo(210+originX,210+originY);
//        mapBound.lineTo(620+originX,210+originY);
//        mapBound.lineTo(620+originX,0+originY);
//        mapBound.lineTo(710+originX,0+originY);
//        mapBound.lineTo(710+originX,480+originY);
//        mapBound.lineTo(620+originX,480+originY);
//        mapBound.lineTo(620+originX,270+originY);
//        mapBound.lineTo(210+originX,270+originY);
//        mapBound.lineTo(0+originX,270+originY);
//        mapBound.lineTo(originX,originY);//结束
//        canvas.drawPath(mapBound,mPaintA);

        //目的地导航
        if (setDestination == true){
            destinationPath.reset();
            destinationPath.moveTo(nowStepX+lastStepX, nowStepY+lastStepY);
//           if (screenMove()<0){
//               destinationPath.lineTo(destination_X + screenMove(),destination_Y);
//               Log.d("目的地","\n------目的地："+destination_X+screenMove()+"    ");
//           }
//           else
               destinationPath.lineTo(destination_X,destination_Y);
            canvas.drawPath(destinationPath,mPaintD);
        }

        //更新行走坐标
        lastStepX = nowStepX+lastStepX;
        lastStepY = nowStepY+lastStepY;

//        lastStepXOfArrow = ofArrow[0];
//        lastStepXOfArrow = ofArrow[1];
        canvas.drawPath(mPath, mPaintC);

//        canvas.drawText("H:"+getHeight()+" W:"+getWidth(),50,50,mPaintA);
        canvas.drawText("空白测试环境,于MyView.OnDraw中绘制具体平面图",50,95,mPaintC);


//        stepFlag = false;
        mPhase += 10;
    }

    public float getNowStepX() {
        return nowStepX;
    }

    public void setNowStepX(float nowStepX) {
        this.nowStepX = nowStepX;
    }

    public float getNowStepY() {
        return nowStepY;
    }

    public void setNowStepY(float nowStepY) {
        this.nowStepY = nowStepY;
    }

    public float getDestination_X() {
        return destination_X;
    }

    public float getDestination_Y() {
        return destination_Y;
    }

    public void setDestination(float X, float Y){
        this.destination_X = X;
        this.destination_Y = Y;
    }

    public float getMapX(float x, float y){
        double mapAngle = Math.toDegrees(this.angle);
        return (float) (x*Math.cos(mapAngle) - y*Math.sin(mapAngle));
    }

    public float getMapY(float x, float y){
        double mapAngle = Math.toDegrees(this.angle);
        return (float) (y*Math.cos(mapAngle) - x*Math.sin(mapAngle));
    }

    public float getOriginX() {
        return originX;
    }

    public float getOriginY() {
        return originY;
    }


    public float getLastStepX() {
        return lastStepX;
    }

    public void setLastStepX(float lastStepX) {
        this.lastStepX = lastStepX;
    }

    public float getLastStepY() {
        return lastStepY;
    }

    public void setLastStepY(float lastStepY) {
        this.lastStepY = lastStepY;
    }


    public boolean isSetDestination() {
        return setDestination;
    }

    public void setSetDestination(boolean setDestination) {
        this.setDestination = setDestination;
    }
}
